﻿// ******************************************************************************
// Developed by Oguz Demirel
// Date: March 26, 2010
// ******************************************************************************
namespace BulkListActions
{
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Security;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;
    using System;
    using System.Globalization;
    using System.Security.Permissions;
    using System.Web;
    using System.Web.UI;
    using System.Collections.Specialized;
    using System.Net;
    using Microsoft.SharePoint.Utilities;

    public class BulkActionsMenu : ToolBarMenuButton
    {
        #region ..:: Properties ::..
        protected string m_DefaultAction;
        protected int m_largeIconThreshold;

        public int LargeIconThreshold
        {
            get
            { return this.m_largeIconThreshold; }
            set
            { this.m_largeIconThreshold = value; }
        }

        public override string Text
        {
            get
            {
                //Show Menu Text, also and get from resource file
                return "Bulk Actions";
            }
        }

        public override bool Visible
        {
            get
            {
                return ((base.RenderContext.List.BaseTemplate != SPListTemplateType.Comments) && base.Visible);
            }

            [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
            set
            {
                base.Visible = value;
            }

        }
        #endregion

        #region ..:: Constructor ::..
        public BulkActionsMenu()
        {
            this.ID = "BulkActionsMenu";
            this.m_DefaultAction = string.Empty;
            // this.TemplateName = "OZDocumentLibraryViewToolBar";
        }
        #endregion ..:: Constructor ::..

        #region ..:: Overridden Methods ::..

        public override MenuItemTemplate AddMenuItem(string id, string displayName, string imageUrl, string description, string navigateUrl, string onClickScript)
        {
            MenuItemTemplate template = base.AddMenuItem(id, displayName, imageUrl, description, navigateUrl, onClickScript);
            template.MenuGroupId = 100;
            template.Permissions = SPBasePermissions.EmptyMask | SPBasePermissions.AddListItems;
            template.PermissionContext = PermissionContext.CurrentFolder;
            template.UseShortId = false;

            return template;
        }

        protected override void AddMenuItems()
        {
            // Make items large
            MenuTemplateControl.LargeIconMode = true;
            
            // Add you own menu items here
            // Delete
            DeleteItemsAction delete = new DeleteItemsAction();
            base.AddMenuItem(delete.ID, delete.Text, delete.ImageUrl, delete.Description, delete.ClientOnClickNavigateUrl, GenerateClientScript(delete.actionName, delete.handlerFileName));

            // Check Out
            CheckOutItemsAction co = new CheckOutItemsAction();
            base.AddMenuItem(co.ID, co.Text, co.ImageUrl, co.Description, co.ClientOnClickNavigateUrl, GenerateClientScript(co.actionName, co.handlerFileName));

            // Check In
            CheckInItemsAction ci = new CheckInItemsAction();
            base.AddMenuItem(ci.ID, ci.Text, ci.ImageUrl, ci.Description, ci.ClientOnClickNavigateUrl, GenerateClientScript(ci.actionName, ci.handlerFileName));

            // Publish
            PublishItemsAction publish = new PublishItemsAction();
            base.AddMenuItem(publish.ID, publish.Text, publish.ImageUrl, publish.Description, publish.ClientOnClickNavigateUrl, GenerateClientScript(publish.actionName, publish.handlerFileName));

            // Unpublish
            UnpublishItemsAction unpublish = new UnpublishItemsAction();
            base.AddMenuItem(unpublish.ID, unpublish.Text, unpublish.ImageUrl, unpublish.Description, unpublish.ClientOnClickNavigateUrl, GenerateClientScript(unpublish.actionName, unpublish.handlerFileName));

            // Update Metadata
            UpdateMetaDataAction metadata = new UpdateMetaDataAction();
            base.AddMenuItem(metadata.ID, metadata.Text, metadata.ImageUrl, metadata.Description, metadata.ClientOnClickNavigateUrl, GenerateClientScript(metadata.actionName, metadata.handlerFileName));

            // Move
            MoveItemsAction move = new MoveItemsAction();
            base.AddMenuItem(move.ID, move.Text, move.ImageUrl, move.Description, move.ClientOnClickNavigateUrl, GenerateClientScript(move.actionName, move.handlerFileName)); 
        }

        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if (!string.IsNullOrEmpty(this.m_DefaultAction))
            {
                base.MenuControl.MenuFormat = MenuFormat.ArrowSplitButton;
                base.MenuControl.ClientOnClickScript = this.m_DefaultAction;
                base.MenuControl.HoverCellInActiveCssClass = "ms-splitbutton";
                base.MenuControl.HoverCellActiveCssClass = "ms-splitbuttonhover";
            }
        }

        #endregion ..:: Rendering Methods ::..

        #region ..:: Private Methods ::..
        private string GenerateClientScript(string action, string actionHandler)
        {
            string str = string.Format(CultureInfo.InvariantCulture, "{0}/_layouts/Zieglers/BulkListActions/" + actionHandler + "?List={1}&View={2}&Item=", new object[] { SPContext.Current.Web.Url, this.ListId.ToString(), this.ViewId.ToString() });
            string str2 = string.Format(CultureInfo.InvariantCulture, "WebPart{0}", new object[] { "WPQ1" });
            string str3 = string.Format(CultureInfo.InvariantCulture, "window.location = '{0}' + GetSelectedItemsString('{1}') + '&referrer=' + escape(window.location)", new object[] { str, str2 });
            return string.Format(CultureInfo.InvariantCulture, "if(ListItemSelection_ConfirmAction('{0}', '{2}')){{{1}}};", new object[] { str2, str3, action });
        }
        #endregion ..:: Private Methods ::..

    }

}
